package com.inf.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.transform.Result;
import java.sql.*;
import java.util.Properties;

/**
 * Created by zhangbn on 2018/11/29.
 */
public class PhoenixUtil {

    static Logger log = LoggerFactory.getLogger(PhoenixUtil.class);

    private static Connection connection = null;

    public static Connection getConnection() {
        if (connection == null) {
            synchronized (PhoenixUtil.class) {
                if (connection == null) {
                    try {
                        Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
                        Properties props = new Properties();
                        props.setProperty("phoenix.schema.isNamespaceMappingEnabled", "true");
                        props.setProperty("phoenix.schema.mapSystemTablesToNamespace", "true");
                        connection = DriverManager.getConnection("jdbc:phoenix:hsmaster:2181", props);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return connection;
    }

    public static ResultSet executeQuery(String sql) {
        Statement statement = null;
        ResultSet rs = null;
        try {
            statement = getConnection().createStatement();
            rs = statement.executeQuery(sql);
        } catch (Exception e) {
            log.error("show: Phoenix查询异常：" + e.toString());
        } finally {
            try {
                //statement.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return rs;
    }






    /**
     * 测试连接Phoenix
     *
     * @param args
     */
    public static void main(String args[]) {
        Connection connection = null;
        Statement statement = null;
        try {
            Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
            Properties props = new Properties();
            props.setProperty("phoenix.schema.isNamespaceMappingEnabled", "true");
            props.setProperty("phoenix.schema.mapSystemTablesToNamespace", "true");
            connection = DriverManager.getConnection("jdbc:phoenix:hsmaster:2181", props);
            statement = connection.createStatement();
            String sql = "select count(\"rowkey\") as uuid from \"bigdata\".\"person-repo\" where \"rowkey\"<'180721235000'";
            long time = System.currentTimeMillis();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                int count = rs.getInt("UUID");
                System.out.println("row count is " + count);
            }
            long timeUsed = System.currentTimeMillis() - time;
            System.out.println("time " + timeUsed + "mm");
            // 关闭连接
            rs.close();
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                connection.close();
                statement.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
